package com.zzsy.carosshopprojects.activitys.home.mainpage.orderamount;

import android.content.Context;
import android.view.View;
import android.widget.LinearLayout;

import com.yatoooon.screenadaptation.ScreenAdapterTools;
import com.zzsy.carosshopprojects.R;
import com.zzsy.carosshopprojects.adapter.OrderReghterAdapter;
import com.zzsy.carosshopprojects.base.BaseFragment;
import com.zzsy.carosshopprojects.bean.ReghterOrderBean;
import com.zzsy.carosshopprojects.view.refreshview.RefreshView;
import com.zzsy.carosshopprojects.view.refreshview.SpacesItemDecoration;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;

/**
 * 吧台订单金额
 */
public class OrderReghterAmount extends BaseFragment implements RefreshView.RefreshHelper {
    public static OrderReghterAmount newInstance() {
        OrderReghterAmount orderReghterAmount = new OrderReghterAmount();
        return orderReghterAmount;
    }

    @BindView(R.id.smartRefresh)
    RefreshView smartRefresh;
    @BindView(R.id.recycleView)
    RecyclerView recycleView;
    @BindView(R.id.no_data_view)
    LinearLayout emptyView;
    private OrderReghterAmountMode orderReghterAmountMode;
    private int page = 1;
    private boolean isShowEmpty = true;
    private boolean isShowDialog = true;
    private int kind = 0;//0 下拉刷新 1 加载更多
    List<ReghterOrderBean.OrdersBean> listData;
    private OrderReghterAdapter orderReghterAdapter;

    @Override
    protected int initLayout() {
        return R.layout.fg_orderreghter;
    }

    @Override
    protected void initView(View view) {
        orderReghterAdapter = new OrderReghterAdapter(getContext(), null, 0);
        listData = new ArrayList<>();
        smartRefresh.setRefreshHelper(this);
        initTitle(view, getResources().getString(R.string.TotalReceipts), getActivity());
        recycleView.setLayoutManager(new LinearLayoutManager(context));
        int space = ScreenAdapterTools.getInstance().loadCustomAttrValue(20);
        recycleView.addItemDecoration(new SpacesItemDecoration(space));
        recycleView.setAdapter(orderReghterAdapter);
        orderReghterAdapter.setOnItemClickListener((v, position) -> {
            start(OrderReghterAmountDetailFragment.newInstance(orderReghterAdapter.getListData().get(position).getRechargeNumber()));
        });
    }

    @Override
    protected void initData(Context mContext) {
        orderReghterAmountMode = getViewModelProvider().get(OrderReghterAmountMode.class);
    }

    @Override
    protected void loadMethod() {
        showData();
    }

    /**
     * 展示列表数据
     */
    private void showData() {
        orderReghterAmountMode.getLiveData().observe(this, orderBeans -> {
            if (orderBeans == null) {
                smartRefresh.finishState();
                return;
            }
            listData.addAll(orderBeans.getOrders());
            orderReghterAdapter.setListData(listData);
            orderReghterAdapter.notifyDataSetChanged();
            if (!isShowDialog) {
                switch (kind) {
                    case 0:
                        smartRefresh.finishRefresh();
                        break;
                    case 1:
                        smartRefresh.finishLoadMore();
                        if (orderBeans != null && orderBeans.getOrders().size() > 0)
                        {}
                        else
                            smartRefresh.finishLoadMoreWithNoMoreData();
                        break;
                }
            }
            setEmptyData();
        });
        orderReghterAmountMode.getOrdersByTerminalId(getContext(), page + "", isShowDialog);
    }

    /**
     * 如果没用数据是否显示空View
     */
    private void setEmptyData() {
        if (isShowEmpty) {
            if (listData == null || listData.size() == 0) {
                emptyView.setVisibility(View.VISIBLE);
            } else {
                emptyView.setVisibility(View.GONE);
            }
        }
    }

    @Override
    public void onRefresh() {
        listData.clear();
        orderReghterAdapter.notifyDataSetChanged();
        kind = 0;
        page = 1;
        isShowEmpty = true;
        isShowDialog = false;
        orderReghterAmountMode.getOrdersByTerminalId(getContext(), page + "", isShowDialog);
    }

    @Override
    public void onLoadMore() {
        isShowEmpty = false;
        isShowDialog = false;
        kind = 1;
        page++;
        orderReghterAmountMode.getOrdersByTerminalId(getContext(), page + "", isShowDialog);
    }


}
